package com.salt.modules.sys.service;

import com.salt.common.service.CrudService;
import com.salt.common.utils.IdGen;
import com.salt.modules.sys.entity.AppChildMenu;
import com.salt.modules.sys.entity.User;
import com.salt.modules.sys.mapper.AppChildMenuMapper;
import com.salt.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description 应用菜单(子级菜单)Service
 * @Author salt
 * @Date 2022/1/18
 * @Version 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class AppChildMenuService extends CrudService<AppChildMenuMapper, AppChildMenu> {

    public AppChildMenu find(AppChildMenu appChildMenu) {
        return mapper.find(appChildMenu);
    }

    @Transactional(readOnly = false)
    public void save(AppChildMenu appChildMenu) {
        appChildMenu.setId(IdGen.uuid());
        appChildMenu.setCreateDate(new Date());
        mapper.insert(appChildMenu);
    }

    @Transactional(readOnly = false)
    public void batchSave(List<AppChildMenu> appChildMenuList) {
        mapper.insertBatch(appChildMenuList);
    }

    @Transactional(readOnly = false)
    public void deleteByParentMenuId(String parentMenuId) {
        User user = UserUtils.getUser();
        AppChildMenu appChildMenu = new AppChildMenu(user.getId(), parentMenuId);
        mapper.delete(appChildMenu);
    }

    @Transactional(readOnly = false)
    public void delete(AppChildMenu appChildMenu) {
        appChildMenu.setUserId(UserUtils.getUser().getId());
        mapper.delete(appChildMenu);
    }
}
